﻿using System;
using System.Data;
using System.Data.OleDb;
using System.Text;

namespace TraditionalMalteseCuisine.App_Code.Objects
{
    [Serializable]
    public class Tag : BaseTable
    {
        //*************************************************
        //                Class Members
        //*************************************************
        #region Class Members
        public const string TABLE_NAME = "Tags";

        public const string PARENT_ID_TAG = "parent_" + Tag.ID_TAG;
        public const string TAG_NAME_TAG = "tag_name";

        private int mParentID = -1;
        private string mTagName = string.Empty;
        #endregion
        //*************************************************


        //*************************************************
        //               Static Methods
        //*************************************************
        #region Static Methods
        public static OleDbCommand GetCommand_GetParentTags()
        {
            OleDbCommand oledbCommand = new OleDbCommand();
            oledbCommand.CommandType = CommandType.Text;

            StringBuilder commandText = new StringBuilder();
            commandText.AppendFormat("SELECT * FROM {0} WHERE (", TABLE_NAME);

            commandText.AppendFormat("([{0}] IS NULL) ", PARENT_ID_TAG);

            commandText.Append(") ");
            commandText.AppendFormat("ORDER BY [{0}]", TAG_NAME_TAG);

            oledbCommand.CommandText = commandText.ToString();

            return oledbCommand;
        }

        public static OleDbCommand GetCommand_GetChildTags()
        {
            return GetCommand_GetChildTags(-1);
        }

        public static OleDbCommand GetCommand_GetChildTags(int parent_id)
        {
            OleDbCommand oledbCommand = new OleDbCommand();
            oledbCommand.CommandType = CommandType.Text;

            StringBuilder commandText = new StringBuilder();
            commandText.AppendFormat("SELECT * FROM {0} WHERE (", TABLE_NAME);

            if (parent_id > -1)
            {
                commandText.AppendFormat("([{0}] = @{0}) ", PARENT_ID_TAG);
                oledbCommand.Parameters.AddWithValue(string.Format("@{0}", PARENT_ID_TAG), parent_id);
            }
            else
                commandText.AppendFormat("([{0}] IS NOT NULL) ", PARENT_ID_TAG);

            commandText.Append(") ");
            commandText.AppendFormat("ORDER BY [{0}]", TAG_NAME_TAG);

            oledbCommand.CommandText = commandText.ToString();

            return oledbCommand;
        }
        #endregion
        //*************************************************


        //*************************************************
        //                   Methods
        //*************************************************
        #region Methods
        protected override string GetInsertScriptUsingParameters()
        {
            StringBuilder insertScript = new StringBuilder();

            insertScript.Append("INSERT INTO ");
            insertScript.AppendFormat("{0} ", TABLE_NAME);
            insertScript.AppendFormat("([{0}], [{1}]) ", PARENT_ID_TAG, TAG_NAME_TAG);
            insertScript.Append("VALUES ");
            insertScript.AppendFormat("(@{0}, @{1}) ", PARENT_ID_TAG, TAG_NAME_TAG);

            return insertScript.ToString();
        }

        public override OleDbCommand GetInsertCommand()
        {
            OleDbCommand insertCommand = new OleDbCommand();

            insertCommand.CommandType = CommandType.Text;
            insertCommand.CommandText = GetInsertScriptUsingParameters();

            insertCommand.Parameters.AddWithValue(string.Format("@{0}", PARENT_ID_TAG), ((ParentID > -1) ? (object)ParentID : DBNull.Value));
            insertCommand.Parameters.AddWithValue(string.Format("@{0}", TAG_NAME_TAG), TagName);

            return insertCommand;
        }

        public override BaseTable ReadDataRow(DataRow datarow)
        {
            ID = int.Parse(datarow[ID_TAG].ToString());
            ParentID = int.Parse(datarow[PARENT_ID_TAG].ToString());
            TagName = datarow[TAG_NAME_TAG].ToString();
            InUse = bool.Parse(datarow[IN_USE_TAG].ToString());

            return this;
        }


        public override string ToString()
        {
            return string.Format("{0}", TagName);
        }
        #endregion
        //*************************************************


        //*************************************************
        //                  Properties
        //*************************************************
        #region Properties
        public int ParentID
        {
            get { return mParentID; }
            set { mParentID = value; }
        }

        public string TagName
        {
            get { return mTagName; }
            set { mTagName = value; }
        }
        #endregion
        //*************************************************
    }
}